package com.srr.sugarloaf;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.orjl.networkj.BaseObserverObject;
import com.orjl.networkj.BaseResponseObjectModel;
import com.orjl.networkj.RestSource;
import com.orjl.networkj.RxScheduler;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.reset)
    ImageView reset;
    private ObjectAnimator mCircleAnimator;


    private boolean isSuccessReturn = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        ImmersionBar immersionBar= ImmersionBar.with(this);
        immersionBar.fitsSystemWindows(false)
                .statusBarDarkFont(true)
                .flymeOSStatusBarFontColor(R.color.BLACK)
                .transparentStatusBar()
                .init();

        Typeface face = Typeface.createFromAsset(getAssets(),"fonts/setofont.ttf");


        tvResult.setTypeface(face);
        setAnim();
        mCircleAnimator.start();
        getSweets();

    }

    private void getSweets() {
        isSuccessReturn = false;
        RestSource.getApiService().getSweets()
                .compose(RxScheduler.<BaseResponseObjectModel<List<String>>>Obs_io_main())
                .as(AutoDispose.<BaseResponseObjectModel<List<String>>>autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new BaseObserverObject<List<String>>(this) {
                    @Override
                    public void onSuccess(List<String> strings) {
                        isSuccessReturn = true;

                        tvResult.setText(strings.get(0));

                    }

                    @Override
                    public void onFailure(String code, String msg) {
                        super.onFailure(code, msg);
                        isSuccessReturn = false;
                        ToastUtils.show(msg);
                    }
                });
    }

    @OnClick(R.id.reset)
    public void onViewClicked() {
        if (isSuccessReturn) {
            mCircleAnimator.start();
            getSweets();
        } else {
            ToastUtils.show("请稍等");
        }
    }

    private void setAnim() {
        mCircleAnimator = ObjectAnimator.ofFloat(reset, "rotation", 0.0f, 360.0f);
        mCircleAnimator.setDuration(800);
        mCircleAnimator.setInterpolator(new LinearInterpolator());
        mCircleAnimator.setRepeatCount(-1);
//        mCircleAnimator.setRepeatMode(ObjectAnimator.RESTART);
        mCircleAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                super.onAnimationCancel(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                if (isSuccessReturn) {
                    mCircleAnimator.end();
                } else {
                    mCircleAnimator.resume();
                }
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationPause(Animator animation) {
                super.onAnimationPause(animation);
            }

            @Override
            public void onAnimationResume(Animator animation) {
                super.onAnimationResume(animation);
            }
        });
    }
}
